package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.business.utils.*;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.common.business.utils.WebClientUtil;
import com.dmcb.trade.business.constants.TradeConstant;
import com.dmcb.trade.business.entities.Article;
import com.dmcb.trade.business.mappers.ArticleCrawlMapper;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * UC头条文章导入
 * Created by wangyong on 2017/6/27.
 */
@Service
public class UCTouTiaoMobileCrawler extends BaseService {
    @Autowired
    private ArticleCrawlMapper articleCrawlMapper;
    @Autowired
    private WebClientUtil webClientUtil;

    Logger logger = LoggerFactory.getLogger(UCTouTiaoMobileCrawler.class);


    public JsonResult crawl(String link, Integer authorId) {
        if (StringUtils.isBlank(link) || authorId == 0) {
            return error("尚未输入链接或作者ID");
        }
        int ero = 0;
        int success = 0;
        int page = 1;
        Map<String, Object> maps;

        //http://a.mp.uc.cn/media.html?mid=263005bea62148168b29429c65a8fd3d&uc_biz_str=S:custom%7CC:iflow_ncmt&uc_param_str=frdnsnpfvecpntnwprdssskt&from=
        //http://napi.uc.cn/3/classes/article/categories/wemedia/lists/263005bea62148168b29429c65a8fd3d?_app_id=cbd10b7b69994dca92e04fe00c05b8c2&_fetch=1&_size=5
        //http://a.mp.uc.cn/media?mid=b415168340d9447e8557ceb2e53d3509&from=xss&uc_param_str=frdnsnpfvecpntnwprdssskt&uc_biz_str=
        //http://ff.dayu.com/contents/author/b415168340d9447e8557ceb2e53d3509?biz_id=1002&_size=8&_page=3
        //WebClientUtil.wc = webClientUtil.initWc(false,false,true);
        String mind = link.substring(link.indexOf("mid=") + 4, link.indexOf("&"));
        Document document = webClientUtil.getDoc(link);
        if (document == null) {
            return error("解析网站链接出错");
        }
        Elements elements = document.select("script");
        if (elements.size() == 0) {
            return error("获取参数 biz_id 出错");
        }
        String data;
        Integer biz_id =null;
        for (Element element : elements) {
            data = element.toString();
            if (!data.contains("globalConfig")) {
                continue;
            }
            data = data.substring(data.indexOf("{"), data.lastIndexOf("}") + 1);
            maps = JsonUtil.getResult(data);
            biz_id = (Integer) maps.get("biz_org_id");
            break;
        }
        if (biz_id ==null) {
            return error("获取参数 biz_id 出错");
        }

        while (true) {
            //http://ff.dayu.com/contents/author/b415168340d9447e8557ceb2e53d3509?biz_id=1002&_size=8&_page=1
            String jsonResult = WebUtil.buildRequest("http://ff.dayu.com/contents/author/"+mind+"?biz_id="+biz_id+"&_size=8&_page="+page, false);
            if (StringUtils.isBlank(jsonResult)) {
                break;
            }
            maps = JsonUtil.getResult(jsonResult);
            if (maps == null) {
                logger.info("解析失败，链接：" + link);
                break;
            }
            List<Map<String, Object>> mapList = (List) maps.get("data");
            if (ListUtil.isEmpty(mapList)) {
                break;
            }
            for (Map<String, Object> map : mapList) {
                JsonResult result = null;
                try {
                    result = save(map, authorId,biz_id);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (result.getStatus() != JsonResult.STATUS_SUCCESS) {
                    if (result.getStatus() == JsonResult.STATUS_ERROR) {
                        ero++;
                    }
                    if (ero >= 10) {
                        return error("连续10次导入失败，请核实原因后再导入");
                    }
                    continue;
                }
                ero = 0;
                success++;
            }
            page++;
        }
        return success("成功添加" + success + "条文章");
    }

    public JsonResult save(Map<String, Object> map, int authorId,Integer biz_id) throws UnsupportedEncodingException {
        //http://a.mp.uc.cn/article.html?uc_param_str=frdnsnpfvecpntnwprdssskt&from=media#!wm_cid=213983228030881792!!wm_aid=1cbbf15d31ad40428994e8e6e0b7d4ff!!
        String link = "http://a.mp.uc.cn/article.html?uc_param_str=frdnsnpfvecpntnwprdssskt&from=media#!wm_cid="+map.get("content_id")+"!!wm_aid="+map.get("origin_id")+"!!";
        String contentId= (String) map.get("content_id");
        String title = (String) map.get("title");
        logger.info(title);
        String time = (String) map.get("created_at");
        String dataLink = "http://ff.dayu.com/contents/"+contentId+"?biz_id="+biz_id+"&_fetch_author=1&_fetch_incrs=1";

        Map<String,Object> dataMap = JsonUtil.getResult(WebUtil.buildRequest(dataLink,false));
        dataMap = (Map)dataMap.get("data");
        Map<String ,Object> authorMap = (Map)dataMap.get("_author");
        String authorName = (String) authorMap.get("author_name");
        Map<String,Object> bodyMap = (Map)dataMap.get("body");
        String content = (String)bodyMap.get("text");
        content = content.replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", "").replaceAll(" ", "");
        // 校验该作者标题是否存在
        Article article = articleCrawlMapper.queryByAuthorTitle(authorId, title);
        if (article == null) {
            // 新文章
            article = new Article();
            article.setAuthorId(authorId);
            article.setTitle(EncodeUtil.filterOffUtf8Mb4(title));
            article.setAuthorName(authorName);
            article.setLink(link);
            article.setPostTime(DateUtil.parseDate(time));
        }
        // 如果已经存在，直接返回
        if (article.getId() > 0) {
            logger.info(TradeConstant.MSG_ERROR_TITLE_EXIST);
            return warn("", "");
        }
        article.setBody(EncodeUtil.filterOffUtf8Mb4(content));
        articleCrawlMapper.insert(article);
        return success();
    }

}
